//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LossDist.h"
using namespace Cephei::QL::Experimental::Credit;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CLossDist::CLossDist (boost::shared_ptr<QuantLib::LossDist>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLossDist = NULL;
#endif
	_ppLossDist = &childNative;
    
}
Cephei::QL::Experimental::Credit::CLossDist::CLossDist (QuantLib::LossDist& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLossDist = NULL;
#endif
	_ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (&childNative);
    
    _LossDistOwner = owner;
    
}

Cephei::QL::Experimental::Credit::CLossDist::CLossDist (CLossDist^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLossDist = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::CLossDist::CLossDist (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLossDist = NULL;
#endif
	if (!t->IsSubclassOf(CLossDist::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CLossDist::CLossDist (QuantLib::Handle<QuantLib::LossDist>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLossDist = &childNative;
	_ppLossDist = &static_cast<boost::shared_ptr<QuantLib::LossDist>>(childNative.currentLink());
    
    _LossDistOwner = owner;
}
Cephei::QL::Experimental::Credit::CLossDist::CLossDist (QuantLib::Handle<QuantLib::LossDist> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLossDist = &childNative;
	_ppLossDist = &static_cast<boost::shared_ptr<QuantLib::LossDist>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CLossDist::CLossDist (QuantLib::LossDist childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLossDist = NULL;
#endif
	_ppLossDist = new boost::shared_ptr<QuantLib::LossDist> (new QuantLib::LossDist (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::CLossDist::~CLossDist ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLossDist != NULL)
    {
	    delete _ppLossDist;
        _ppLossDist = NULL;
    }
}
Cephei::QL::Experimental::Credit::CLossDist::!CLossDist ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLossDist != NULL)
    {
	    delete _ppLossDist;
    }
}
QuantLib::LossDist& Cephei::QL::Experimental::Credit::CLossDist::GetReference ()
{
    if (_ppLossDist == NULL) throw REFNEW NativeNullException ();
	return **_ppLossDist;
}
boost::shared_ptr<QuantLib::LossDist>& Cephei::QL::Experimental::Credit::CLossDist::GetShared ()
{
    if (_ppLossDist == NULL) throw REFNEW NativeNullException ();
	return *_ppLossDist;
}
QuantLib::LossDist* Cephei::QL::Experimental::Credit::CLossDist::GetPointer ()
{
    if (_ppLossDist == NULL) throw REFNEW NativeNullException ();
	return &**_ppLossDist;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LossDist>& Cephei::QL::Experimental::Credit::CLossDist::GetHandle ()
{
	if (_phLossDist == NULL)
	{
		_phLossDist = new Handle<QuantLib::LossDist> (*_ppLossDist);
	}
	return *_phLossDist;
}
#endif
bool Cephei::QL::Experimental::Credit::CLossDist::HasNative () 
{
	return (_ppLossDist != NULL);
}

Double Cephei::QL::Experimental::Credit::CLossDist::BinomialProbabilityOfAtLeastNEvents (Int32 n, Cephei::Core::IVector<Double>^ p)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        int _n = (int)ValueHelper::Convert (n); //a
        p->Lock();
        INativeVector<Double>^ _NCIp = p->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCp = safe_cast<CDoubleVector^>(_NCIp);
        std::vector<QuantLib::Real>& _p = static_cast<std::vector<QuantLib::Real>&> (_NCp->GetReference ());
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLossDist)->binomialProbabilityOfAtLeastNEvents ( _n,  _p );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (p != nullptr) p->Unlock(); //NO2  
    }
}
Double Cephei::QL::Experimental::Credit::CLossDist::BinomialProbabilityOfNEvents (Int32 n, Cephei::Core::IVector<Double>^ p)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        int _n = (int)ValueHelper::Convert (n); //a
        p->Lock();
        INativeVector<Double>^ _NCIp = p->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCp = safe_cast<CDoubleVector^>(_NCIp);
        std::vector<QuantLib::Real>& _p = static_cast<std::vector<QuantLib::Real>&> (_NCp->GetReference ());
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLossDist)->binomialProbabilityOfNEvents ( _n,  _p );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (p != nullptr) p->Unlock(); //NO2  
    }
}
UInt64 Cephei::QL::Experimental::Credit::CLossDist::Buckets::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLossDist)->buckets ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CLossDist::Maximum::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLossDist)->maximum ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CLossDist::ProbabilityOfAtLeastNEvents (Int32 n, Cephei::Core::IVector<Double>^ p)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        int _n = (int)ValueHelper::Convert (n); //a
        p->Lock();
        INativeVector<Double>^ _NCIp = p->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCp = safe_cast<CDoubleVector^>(_NCIp);
        std::vector<QuantLib::Real>& _p = static_cast<std::vector<QuantLib::Real>&> (_NCp->GetReference ());
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLossDist)->probabilityOfAtLeastNEvents ( _n,  _p );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (p != nullptr) p->Unlock(); //NO2  
    }
}
Double Cephei::QL::Experimental::Credit::CLossDist::ProbabilityOfNEvents (Int32 n, Cephei::Core::IVector<Double>^ p)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        int _n = (int)ValueHelper::Convert (n); //a
        p->Lock();
        INativeVector<Double>^ _NCIp = p->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCp = safe_cast<CDoubleVector^>(_NCIp);
        std::vector<QuantLib::Real>& _p = static_cast<std::vector<QuantLib::Real>&> (_NCp->GetReference ());
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLossDist)->probabilityOfNEvents ( _n,  _p );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (p != nullptr) p->Unlock(); //NO2  
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Experimental::Credit::CLossDist::ProbabilityOfNEvents (Cephei::Core::IVector<Double>^ p)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        p->Lock();
        INativeVector<Double>^ _NCIp = p->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCp = safe_cast<CDoubleVector^>(_NCIp);
        std::vector<QuantLib::Real>& _p = static_cast<std::vector<QuantLib::Real>&> (_NCp->GetReference ());
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppLossDist)->probabilityOfNEvents ( _p );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (p != nullptr) p->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

